from machine import Pin,I2C

from zk import *

address=63
def lcdInit():
    i2c.writeto(address,b"\x00\x3b\x03\x38\x05\x0c\x39\x08\x10\x80")
    clearScreen()
def setaddress(x,y):
    x=x+4
    buf =b"\x00\x38" + bytes([64+y,0xe0+(x&0x0f),0xf0+((x&0xf0)>>4)])
    i2c.writeto(address,buf)
    
def clearScreen():
    for i in range(0,8):
        setaddress(0,i)
        buff=bytearray(132)
        write(buff)
def write(buf):
    i2c.writeto(address,b"\x40"+buf)

def st(h=0,m=0,y=0,mon=0,d=0,w=0,u=0):
    for i in range(4):
        setaddress(0,i)
        write(n32[int(h/10)][i*16:i*16+15])
        write(n32[int(h%10)][i*16:i*16+15])
        write(n32[10][i*16:i*16+15])
        write(n32[int(m/10)][i*16:i*16+15])
        write(n32[int(m%10)][i*16:i*16+15])
      
    setaddress(80,0)
    write(xj[7][0:15])
    write(xj[8][0:15])
    write(xj[w][0:15])
    setaddress(80,1)
    write(xj[7][16:31])
    write(xj[8][16:31])
    write(xj[w][16:31])
    print8(f"20{y:02d}-{mon:02d}-{d:02d}",0,4)
    print8("%.2fV   "%(u),80,4)

def st1(t=0,r=0):
    
    str="%.2fC  %.2f%%    "%(t,r)

    for n in range(2):
        setaddress(0,n+6)
        for i in str:
            if ord(i)>=48 and ord(i)<=57:
                write(n16[ord(i)-48][n*8:n*8+7])
            if ord(i)== 45 :
                write(n16[10][n*8:n*8+7])  #-
            if ord(i) == 46:
                write(n16[11][n*8:n*8+7])  #.
            if ord(i) == 67:
                write(n16[14][n*8:n*8+7])  #C
            if ord(i) == 86:
                write(n16[12][n*8:n*8+7])  #V
            if ord(i) == 37:
                write(n16[13][n*8:n*8+7]) #%
            if ord(i) == 32:
                write(n16[15][n*8:n*8+7]) #%
def print8(str,x=0,y=0):
    setaddress(x,y)
    for i in str:
        write(ASCII[ord(i)-32]+b'\x00')

if __name__=="__main__":
    import lc
    i2c = I2C(0, scl=Pin(6), sda=Pin(7), freq=400000)

#     i2c = I2C(0,scl=Pin(lc.scl), sda=Pin(lc.sda), freq=400000)#
#     i2c = I2C(1,scl=Pin(18), sda=Pin(19),freq=400000)
    print(i2c.scan())
    lcdInit()
    clearScreen()
    setaddress(0,0)
    write(ASCII[24])
    print(xj[0])
    print(xj[0][0:15])
    print(xj[0][16:31])
    setaddress(0,1)
    write(xj[7][0:15])
    write(xj[8][0:15])
    setaddress(0,2)
    write(xj[7][16:31])
    write(xj[8][16:31])
    st(12,16)
    
    st(12,17,23,9,24,6,4.12)
    st1(-30.65,63)
    